//
//  YHKEmoticonToolBarView.swift
//  xinlangweiBo2
//
//  Created by 杨和逵 on 16/3/7.
//  Copyright © 2016年 yanghekui. All rights reserved.
//

import UIKit

//定义一个枚举，用来记录tag
enum YHKEmoticonToolBarButtonType: Int{
    
    case Recent = 0
    case Default = 1
    case Emoji = 2
    case Lxh = 3
}


//设置协议
protocol YHKEmoticonToolBarViewDelgate: NSObjectProtocol{
    
    //方法
    func didToolBarItmeClick(kype:YHKEmoticonToolBarButtonType)
    
}


class YHKEmoticonToolBarView: UIView {
    
    //声明一个代理属性
    weak var delgate: YHKEmoticonToolBarViewDelgate?


    //三部曲
    //init方法
   override init(frame: CGRect) {
        super.init(frame: frame)
        setUpView()
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    //私有方法，添加约束和视图
    private func setUpView(){
        
        //添加视图
        addSubview(recentButton)
        addSubview(defaultButton)
        addSubview(emojiButton)
        addSubview(lxhButton)
        
        //设置约束
        //第一个按钮
        recentButton.snp_makeConstraints { (make) -> Void in
            //左 上 下 宽=defaultButton
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(self.snp_bottom)
            make.width.equalTo(defaultButton.snp_width)
        }
        
        //第二个按钮
        defaultButton.snp_makeConstraints { (make) -> Void in
            //左：recentButton 上 下 宽：emojiButton
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(self.snp_bottom)
            make.left.equalTo(recentButton.snp_right)
            make.width.equalTo(emojiButton.snp_width)
            
        }
        
        //第san个按钮
        emojiButton.snp_makeConstraints { (make) -> Void in
            // 上 下 左：defaultButton  宽lxhButton
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(self.snp_bottom)
            make.left.equalTo(defaultButton.snp_right)
            make.width.equalTo(lxhButton.snp_width)
        }
        
        //第四个按钮
        lxhButton.snp_makeConstraints { (make) -> Void in
            //上 下  左 右 
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(self.snp_bottom)
            make.left.equalTo(emojiButton.snp_right)
            make.right.equalTo(self.snp_right)
        }
        
        
    }
    
    //MARK：--懒加载
    //常用表情
    private lazy var recentButton:UIButton = {
    
        let btn = UIButton()
        btn.setTitle("最近", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_normal"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_selected"), forState: .Selected)
        btn.addTarget(self, action: "changeEmoticon:", forControlEvents: .TouchUpInside)
        
        //记录tag
        btn.tag = YHKEmoticonToolBarButtonType.Recent.rawValue
        
        return btn
    
    }()
    
    
    //默认表情
    private lazy var defaultButton:UIButton = {
        
        let btn = UIButton()
        btn.setTitle("默认", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_normal"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_selected"), forState: .Selected)
        btn.addTarget(self, action: "changeEmoticon:", forControlEvents: .TouchUpInside)
         //记录tag
        btn.tag = YHKEmoticonToolBarButtonType.Default.rawValue
        
        return btn
        
    }()

    
    
    
    
    //emoji表情
    private lazy var emojiButton:UIButton = {
        
        let btn = UIButton()
        btn.setTitle("emoji", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_normal"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_selected"), forState: .Selected)
        btn.addTarget(self, action: "changeEmoticon:", forControlEvents: .TouchUpInside)
        
        //记录tag
        btn.tag = YHKEmoticonToolBarButtonType.Emoji.rawValue
        
        return btn
        
    }()
    
    
    //浪小花
    private lazy var lxhButton:UIButton = {
        
        let btn = UIButton()
        btn.setTitle("浪小花", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_normal"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_selected"), forState: .Selected)
        btn.addTarget(self, action: "changeEmoticon:", forControlEvents: .TouchUpInside)
        
        //记录tag
        btn.tag = YHKEmoticonToolBarButtonType.Lxh.rawValue
        
        return btn
        
    }()

    //按钮的点击事件
    @objc private func changeEmoticon(button:UIButton){
        
        //先让所有的按钮的选中状态为false
        for subView in self.subviews{
            
            let btn = subView as! UIButton
            
            btn.selected = false
        }
        
        button.selected = true
        
        
        
        //调用一下代理方法
//        delgate?.didToolBarItemClick(YHKEmoticonToolBarButtonType(rawValue: button.tag)!)
        delgate?.didToolBarItmeClick(YHKEmoticonToolBarButtonType(rawValue: button.tag)!)
        
    }
    
    
    
}
